﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using InSafetyAirClient.InSafetyAirService;

namespace InSafetyAirClient.GUI.AccountPerson
{
    public partial class UpdateAP : UserControl
    {
        public UpdateAP()
        {
            InitializeComponent();
        }

        private static List<Person> persons = null;
        private static List<Account> accounts = null;
        private AutoCompleteStringCollection autoComplete = new AutoCompleteStringCollection();


        private void UpdateAP_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                ServiceSingle servS = ServiceSingle.Instance;
                if (!this.DesignMode)
                {
                    persons = servS.getService().GetAll(typeof(Person).Name).OfType<Person>().ToList();
                    foreach (Person person in persons)
                    {
                        autoComplete.Add(person.fName + " " + person.sName);
                        comboBox1.Items.Add(person.fName + " " + person.sName);
                    }
                    comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    comboBox1.AutoCompleteCustomSource = autoComplete;
                }
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            ServiceSingle servS = ServiceSingle.Instance;
            accounts = servS.getService().GetAll(typeof(InSafetyAirService.Account).Name).OfType<InSafetyAirService.Account>().ToList();
            foreach (InSafetyAirService.Person pers in persons)
            {
                String nume=pers.fName+" "+ pers.sName;
                if(nume.Equals(comboBox1.Text))
                {
                    textBox1.Text = pers.fName;
                    textBox2.Text = pers.sName;
                    if(pers.gender==true)
                        comboBox3.Text="Male";
                    else
                        comboBox3.Text="Female";
                    textBox3.Text = pers.passportNo;
                    textBox4.Text = pers.address;
                    foreach (InSafetyAirService.Account account in accounts)
                    {
                        if (account.personId == pers.personId)
                        {
                            textBox5.Text = account.username;
                            textBox8.Text = account.email;
                            comboBox10.Text = account.access+"";
                        }
                    }

                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ServiceSingle servS = ServiceSingle.Instance;
            accounts = servS.getService().GetAll(typeof(InSafetyAirService.Account).Name).OfType<InSafetyAirService.Account>().ToList();
            foreach (InSafetyAirService.Person pers in persons)
            {
                String nume = pers.fName + " " + pers.sName;
                if (nume.Equals(comboBox1.Text))
                {
                    pers.fName = textBox1.Text;
                    pers.sName = textBox2.Text;
                    if (comboBox3.Text.Equals("Male"))
                        pers.gender=true;
                    else
                        pers.gender=false;
                    pers.passportNo = textBox3.Text;
                    pers.address = textBox4.Text;
                    ServiceSingle.Instance.getService().updatePerson(pers);
                }
            }
        }
    }
}
